package com.xyzisme76.application;

import com.xyzisme76.dao.UserDao;
import com.xyzisme76.pojo.User;
import com.xyzisme76.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import javax.imageio.ImageIO;
import javax.swing.*;


class Opacity {
    JFrame jf = new JFrame("广东外语外贸大学" );
    public void init() {
        // 设置大小 影响到图片格式
        jf.setSize(new Dimension(800,400));
        // 所在位置 居中
        jf.setLocationRelativeTo(null);
        //设置背景图片
        JPanel jp = new JPanel() {
            protected void paintComponent(Graphics g) {
                try {
                    Image bg = ImageIO.read(new File("src/main/java/com/xyzisme76/application/images/loginback.png"));
                    g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
        };
        //页面不可以调整大小

        jf.setResizable(false);

        jf.setContentPane(jp);

        jp.setLayout(new GridBagLayout());
        // 欢迎用语标题
        JLabel jl = new JLabel(" 学生成绩管理系统");
        // 登入 button
        ImageIcon icon = new ImageIcon("src/main/java/com/xyzisme76/application/images/loginbutton.png");
        // 创建按钮
        JButton jb = new JButton(icon);
        // 用户名输入框
        JLabel l1 = new JLabel(" 账号:");
        JTextField jTextField = new JTextField(25);
        // 密码输入框
        JLabel l2 = new JLabel(" 密码:");
        JPasswordField jPasswordField = new JPasswordField(25);

        jp.setLayout(null);
        // 绝对位置
        jb.setBounds(510, 270, 91, 29);
        jl.setBounds(390,25,500,100);
        l1.setBounds(380,105,400,100);
        l2.setBounds(380,165,400,100);
        jPasswordField.setBounds(450,200,230,30);
        jTextField.setBounds(450,140,230,30);

        jb.setBackground(Color.white);
        jl.setForeground(Color.DARK_GRAY);
        jl.setFont(new Font("楷体", Font.BOLD, 35));
        l1.setForeground(Color.DARK_GRAY);
        l1.setFont(new Font("楷体", Font.BOLD, 20));
        l2.setForeground(Color.DARK_GRAY);
        l2.setFont(new Font("楷体", Font.BOLD, 20));
        jTextField.setForeground(Color.DARK_GRAY);
        jTextField.setFont(new Font("微软雅黑", Font.BOLD, 15));

        jp.add(jTextField);
        jp.add(jl);
        jp.add(jPasswordField);
        jp.add(jb);
        jp.add(l1);
        jp.add(l2);
        //J button 事件
        jb.addActionListener(e -> {
            // 获取到的事件源就是按钮本身
            // JButton btn = (JButton) e.getSource();
            char[] cs = jPasswordField.getPassword();
            String psw = new String(cs);
            String nameW = jTextField.getText();

            System.out.println(psw + nameW);
            System.out.println("按钮被点击");

            if(CheckLogin.check(nameW,psw) == 1){
                //JOptionPane.showMessageDialog(null,"登入成功！");
                jf.dispose();
                Menu menu = new Menu(CheckLogin.userRoot);
            }
            else {
                JOptionPane.showMessageDialog(null,"登入失败！");
            };
        });

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Opacity().init();
        //Menu menu = new Menu(1);
        // new SqlSelect();
        //new SqlSelectInfo();
        //new RootIndex();
        //new Statistical();
    }
}

//检查登入
class CheckLogin{
    static int userRoot = 0;
    static public int check(String nameW, String pwd){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = userDao.getUserByName(nameW);
        if(user == null) {
            sqlSession.close();
            return 0;
        }
        else if(Objects.equals(user.getPwd(), pwd)){
            if(user.getId()>=20000) {
                userRoot = 2;
            }else{
                userRoot = 1;
            }
            sqlSession.close();
            return 1;
        }
        sqlSession.close();
        return 0;
    }
}
